package xinyu.customer.login;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;
import xinyu.customer.R;
import xinyu.customer.base.AppBaseActivity;
import xinyu.customer.dialog.SecretDialog;
import com.logos.common.location.service.LocationService;
import com.logos.common.widget.display.StatusBarHelper;

import xinyu.customer.permission.MPermission;
import xinyu.customer.permission.annotation.OnMPermissionDenied;
import xinyu.customer.permission.annotation.OnMPermissionGranted;
import xinyu.customer.permission.annotation.OnMPermissionNeverAskAgain;
import xinyu.customer.util.SPUtils;
import xinyu.customer.util.SpConstant;
import xinyu.customer.widget.CustomToastUtil;

/**
 * 选择登录方式界面
 */
public class SelectLoginActivity extends AppBaseActivity {
    @BindView(R.id.ll_phone_login)
    LinearLayout llPhoneLogin;
    @BindView(R.id.ll_weixin_login)
    LinearLayout llWeixinLogin;
    @BindView(R.id.tv_to_register)
    TextView tvToRegister;

    /**
     * 基本权限管理
     */
    private final String[] BASIC_PERMISSIONS = new String[]{
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION
    };

    private static final int BASIC_PERMISSION_REQUEST_CODE = 100;
    private LocationService locationService;
    private boolean isFirstLoc = true;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        StatusBarHelper.setStatusBarDarkMode(this);
        setContentView(R.layout.activity_select_login);
        ButterKnife.bind(this);

        initView();
    }

    private void initView() {
        boolean isFirst = SpConstant.isFirstInstall();
        if (isFirst){
            SecretDialog  dialog = new SecretDialog(this);
            dialog.show();
            dialog.setLisener(new SecretDialog.OnDialogDissmissLisener() {
                @Override
                public void onDialogClick(boolean confirm) {
                    if (confirm){
                        requestBasicPermission();
                        SpConstant.setFirstInstall(false);
                    }else {//不同意
                        moveTaskToBack(false);
                    }
                }
            });
        }
    }


    private void requestBasicPermission() {
        MPermission.printMPermissionResult(true, this, BASIC_PERMISSIONS);
        MPermission.with(this)
                .setRequestCode(BASIC_PERMISSION_REQUEST_CODE)
                .permissions(BASIC_PERMISSIONS)
                .request();
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        MPermission.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

    @OnMPermissionGranted(BASIC_PERMISSION_REQUEST_CODE)
    public void onBasicPermissionSuccess() {
        MPermission.printMPermissionResult(false, this, BASIC_PERMISSIONS);
        initLocationSEervice();
    }

    @OnMPermissionDenied(BASIC_PERMISSION_REQUEST_CODE)
    @OnMPermissionNeverAskAgain(BASIC_PERMISSION_REQUEST_CODE)
    public void onBasicPermissionFailed() {
        try {
            CustomToastUtil.showToast("权限未全部授权，部分功能可能无法正常运行！");
        } catch (Exception e) {
            e.printStackTrace();
        }
        MPermission.printMPermissionResult(false, this, BASIC_PERMISSIONS);
    }


    private void initLocationSEervice(){
        locationService = new LocationService(SelectLoginActivity.this);
        locationService.registerListener(mListener);
        locationService.start();

    }

    private BDLocationListener mListener = new BDLocationListener() {
        @Override
        public void onReceiveLocation(BDLocation location) {
            if (location != null  ) {
                if (location.getLocType() == BDLocation.TypeServerError){
                    return;
                }
                if (isFirstLoc) {
                    if (!TextUtils.isEmpty(location.getCity())) {
                        SPUtils.put(SelectLoginActivity.this, SpConstant.KEY_USER_LAT, location.getLatitude() + "");
                        SPUtils.put(SelectLoginActivity.this, SpConstant.KEY_USER_LNG, location.getLongitude() + "");
                        SPUtils.put(SelectLoginActivity.this, SpConstant.KEY_USER_BD_PROVINCE, location.getProvince() + "");
                        SPUtils.put(SelectLoginActivity.this, SpConstant.KEY_USER_BD_CITY, location.getCity() + "");
                        isFirstLoc = false;
                        locationService.stop();
                    }
                }
            }
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (locationService!=null){
            locationService.unregisterListener(mListener);
            locationService.stop();
        }

    }
    @OnClick({R.id.ll_phone_login, R.id.ll_weixin_login, R.id.tv_to_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_phone_login:
                startActivity(new Intent(SelectLoginActivity.this,LoginActivity.class));
                break;
            case R.id.ll_weixin_login:
                break;
            case R.id.tv_to_register:
                break;
        }
    }
}
